import {isEmpty, showComponentToBody} from "@utils";
import {defineComponent, h} from "vue";
import {Overlay, Toast} from 'vant';
import storage from "@/store/storage";

let component: any = undefined

function showComponent(msg: string) {
  component = showComponentToBody(h(comp, {msg}));
}

export default function (msg: string) {
  if (isEmpty(component)) {
    showComponent(msg);
  }
};

const comp = defineComponent({
  props: {
    msg: {
      type: String,
      default: '请重新登录'
    }
  },
  mounted() {
    storage.logout();
    setTimeout(() => {
      this.$emit('close')
      component = undefined;
      location.reload()
    }, 1200)
  },
  render() {
    return h(Overlay, {
      show: true,
      style: {
        background: 'rgba(255,255,255,0)'
      }
    }, {
      default: () => h(Toast, {
        icon: 'fail',
        show: true,
        message: this.msg,
      })
    })
  },
});